package com.example.administrator.scan2web;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.google.zxing.Result;
import com.google.zxing.android.CaptureActivity;

public class MainActivity extends CaptureActivity {

    private ProgressDialog mProgress;
    private String path;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getContentView() {
        return R.layout.activity_main;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        if (resultCode != Activity.RESULT_OK || requestCode != 100 || data == null) {
//            return;
//        }

        Bundle extras = data.getExtras();
        if (extras != null) {
            String thumbnail = extras.getString("thumb");
            String imgs[] = extras.getStringArray("imgs");

            //如果缩略图有数据，表示只选择了单张图片（拍照裁切后的图片）
            if (!TextUtils.isEmpty(thumbnail)) {
                path = thumbnail;
            } else if (imgs != null && imgs.length > 0) {
                path = imgs[0];
            }

            mProgress = new ProgressDialog(this);
            mProgress.setMessage("正在扫描...");
            mProgress.setCancelable(false);
            mProgress.show();

            new Thread() {
                @Override
                public void run() {
                    final Result result = scanningImage(path);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mProgress.dismiss();
                            if (result == null) {
                                Toast.makeText(MainActivity.this, "扫描失败！", Toast.LENGTH_LONG).show();
                            } else {

                                Log.e("test","one");
                                handleDecode(result, null, 0);
                            }
                        }
                    });
                }
            }.start();
        }
    }
}
